package com.jason.lightreader.controller;

import com.jason.lightreader.entity.User;
import com.jason.lightreader.response.Code;
import com.jason.lightreader.response.Result;
import com.jason.lightreader.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @PreAuthorize("isAnonymous()")
    public Result login(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/register")
    @PreAuthorize("isAnonymous()")
    public Result register(@RequestBody User user) {
        return userService.register(user);
    }

    @GetMapping("/self")
    @PreAuthorize("isAuthenticated()")
    public Result getUserDetails() {
        return userService.getSelfDetails();
    }

    @GetMapping("/test")
    @PreAuthorize("permitAll()")
    public Result testConnect() {
        return Result.get(Code.SUCCEED, "连接成功！");
    }
}
